home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / pack / xfh132.lzh / XFH / src / dosfunc.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  21KB  |  704 lines

  1. /* dosfunc.c - packet implementations of standard dos functions
  2.    with variations to suit the XFH filing system.
  3.    Copyright (C) 1991, 1992, 1993 Kristian Nielsen.
  4.  
  5.    This file is part of XFH, the compressing file system handler.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            */
  20.  
  21. /* $Log:    dosfunc.c,v $
  22.  * Revision 1.2  93/01/14  15:28:59  Kristian
  23.  * Added RCS keywords.
  24.  * 
  25.  */
  26.  
  27. #include "CFS.h"
  28.  
  29. #include <exec/ports.h>
  30.  
  31. #include <string.h>
  32.  
  33. #include <dossupport.h>
  34.  
  35.  
  36.  
  37. /* Lock a file. Passed a parent-lock, a file name, and a mode. */
  38.  
  39. struct FileLock *xLock(glb glob, struct FileLock *parent,
  40.                        char *name, LONG mode){
  41.    struct MsgPort *procid;
  42.    LONG res1;
  43.    
  44.    procid = parent ? parent->fl_Task : glob->xprocid;
  45.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  46.                  ACTION_LOCATE_OBJECT, 3,
  47.                  c2b(parent), cstr2b(name,glob->stringbuf), mode );
  48.    return (struct FileLock *) b2c( res1 );
  49. }
  50.  
  51.  
  52. /* Test whether a given file/dir exists. ASSUMES a Lock() on a non-existing
  53.  * object will return ERROR_OBJECT_NOT_FOUND (so if this returns FALSE, one
  54.  * can be pretty sure that the file does not exist).
  55.  * Ceveat: because of race conditions, the returned value cannot be
  56.  * guaranteed to be correct.
  57.  */
  58. BOOL xExists1(glb glob, struct FileLock *parent, char *name){
  59.    struct FileLock *tmplock = xLock(glob, parent, name, ACCESS_READ);
  60.    if(tmplock){
  61.       xUnLock(glob, tmplock);
  62.       return TRUE;
  63.    }else{
  64.       return (glob->ioerr == ERROR_OBJECT_NOT_FOUND) ? FALSE : TRUE;
  65.    }
  66. }
  67.  
  68.  
  69. BOOL xExamine(glb glob, struct FileLock *lock, struct FileInfoBlock *fib){
  70.    struct MsgPort *procid;
  71.    LONG res1;
  72.    
  73.    procid = lock ? lock->fl_Task : glob->xprocid;
  74.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  75.                  ACTION_EXAMINE_OBJECT, 2,
  76.                  c2b(lock), c2b(fib) );
  77.                  
  78.    /* Convert bcpl string to c-strings. */
  79.    bstr2cinplace( &fib->fib_FileName[0] );
  80.    bstr2cinplace( &fib->fib_Comment[0] );
  81.    
  82.    return (BOOL) res1;
  83. }
  84.  
  85.  
  86. BOOL xExamineNext(glb glob, struct FileLock *lock, struct FileInfoBlock *fib){
  87.    struct MsgPort *procid;
  88.    LONG res1;
  89.    
  90.    /* Convert c-strings to bcpl strings.
  91.     * According to specs, this is not nessesary for the comment. However,
  92.     * 'better safe that sorry'.
  93.     * However, care must be taken to handle the case where the comment is
  94.     * not a valid BSTR. For example, dos.library messes it up in Examine()
  95.     * and ExNext() (converting it to a CSTR and not converting back).
  96.     */
  97.    cstr2binplace(&fib->fib_FileName[0]);
  98.    safecstr2binplace(&fib->fib_Comment[0], 80);
  99.    
  100.    procid = lock ? lock->fl_Task : glob->xprocid;
  101.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  102.                  ACTION_EXAMINE_NEXT, 2,
  103.                  c2b(lock), c2b(fib) );
  104.                  
  105.    /* Convert bcpl string to c-strings. */
  106.    bstr2cinplace( &fib->fib_FileName[0] );
  107.    bstr2cinplace( &fib->fib_Comment[0] );
  108.    
  109.    return (BOOL) res1;
  110. }
  111.  
  112.  
  113. struct FileLock *xCreateDir(glb glob, struct FileLock *parent, char *name){
  114.    struct MsgPort *procid;
  115.    LONG res1;
  116.    
  117.    procid = parent ? parent->fl_Task : glob->xprocid;
  118.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  119.                  ACTION_CREATE_DIR, 2,
  120.                  c2b(parent), cstr2b(name,glob->stringbuf) );
  121.    return (struct FileLock *) b2c( res1 );
  122. }
  123.  
  124.  
  125. BOOL xDeleteFile(glb glob, struct FileLock *parent, char *name){
  126.    struct MsgPort *procid;
  127.    BOOL res1;
  128.    
  129.    procid = parent ? parent->fl_Task : glob->xprocid;
  130.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  131.                  ACTION_DELETE_OBJECT, 2,
  132.                  c2b(parent), cstr2b(name,glob->stringbuf) );
  133.    return res1;
  134. }
  135.  
  136.  
  137. BOOL xRename(glb glob, struct FileLock *parent1, char *name1,
  138.                        struct FileLock *parent2, char *name2){
  139.    struct MsgPort *procid1, *procid2;
  140.    BOOL res1;
  141.    
  142.    procid1 = parent1 ? parent1->fl_Task : glob->xprocid;
  143.    procid2 = parent2 ? parent2->fl_Task : glob->xprocid;
  144.     if( procid1 != procid2 ){
  145.         debug(("ERROR: xRename() across devices.\n"));
  146.         glob->ioerr = ERROR_RENAME_ACROSS_DEVICES;
  147.         return DOSFALSE;
  148.     }
  149.    res1 = dopkt( &glob->iopkt, procid1, glob->ioport, &glob->ioerr, 
  150.                  ACTION_RENAME_OBJECT, 4,
  151.                  c2b(parent1), cstr2b(name1,glob->stringbuf),
  152.                  c2b(parent2), cstr2b(name2,glob->stringbuf2) );
  153.    return res1;
  154. }
  155.  
  156.  
  157. struct FileLock *xParentDir(glb glob, struct FileLock *lock){
  158.    struct MsgPort *procid;
  159.    LONG res1;
  160.    
  161.    procid = lock ? lock->fl_Task : glob->xprocid;
  162.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  163.                  ACTION_PARENT, 1,
  164.                  c2b(lock) );
  165.    return (struct FileLock *) b2c( res1 );
  166. }
  167.  
  168.  
  169. BOOL xSetProtection(glb glob, struct FileLock *parent, char *name, LONG bits){
  170.    struct MsgPort *procid;
  171.    BOOL res1;
  172.    
  173.    procid = parent ? parent->fl_Task : glob->xprocid;
  174.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  175.                  ACTION_SET_PROTECT, 4,
  176.                  0L, c2b(parent), cstr2b(name,glob->stringbuf), bits );
  177.    return res1;
  178. }
  179.  
  180.  
  181. BOOL xSetComment(glb glob, struct FileLock *parent, char *name, char *comment){
  182.    struct MsgPort *procid;
  183.    BOOL res1;
  184.    
  185.    procid = parent ? parent->fl_Task : glob->xprocid;
  186.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  187.                  ACTION_SET_COMMENT, 4,
  188.                  0L, c2b(parent), cstr2b(name,glob->stringbuf),
  189.                                    cstr2b(comment,glob->stringbuf2) );
  190.    return res1;
  191. }
  192.  
  193.  
  194. BOOL xSetFileDate(glb glob, struct FileLock *parent, char *name, struct DateStamp *ds){
  195.    struct MsgPort *procid;
  196.    BOOL res1;
  197.    
  198.    procid = parent ? parent->fl_Task : glob->xprocid;
  199.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  200.                  ACTION_SET_DATE, 4,
  201.                  0L, c2b(parent), cstr2b(name,glob->stringbuf), ds );
  202.    return res1;
  203. }
  204.  
  205.  
  206. struct FileLock *xDupLock(glb glob, struct FileLock *lock){
  207.    struct MsgPort *procid;
  208.    LONG res1;
  209.    
  210.    procid = lock ? lock->fl_Task : glob->xprocid;
  211.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  212.                  ACTION_COPY_DIR, 1,
  213.                  c2b(lock) );
  214.    return (struct FileLock *) b2c( res1 );
  215. }
  216.  
  217.  
  218. BOOL xUnLock(glb glob, struct FileLock *lock){
  219.    struct MsgPort *procid;
  220.    LONG res1;
  221.    
  222.    procid = lock ? lock->fl_Task : glob->xprocid;
  223.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  224.                  ACTION_FREE_LOCK, 1,
  225.                  c2b(lock) );
  226.    return (BOOL) res1;
  227. }
  228.  
  229.  
  230. /* NOTE: Do NOT call xOpen() with a bogus mode!!! */
  231. struct FileHandle *xOpen(glb glob, struct FileLock *parent,
  232.                        char *name, LONG mode){
  233.    struct MsgPort *procid;
  234.    LONG res1;
  235.    struct FileHandle *fh;
  236.    
  237.    procid = parent ? parent->fl_Task : glob->xprocid;
  238.    if( !dalloc(fh) ){
  239.       OUTOFMEM;
  240.       return NULL;
  241.    }
  242.    fh->fh_Pos=fh->fh_End=-1L;
  243.    fh->fh_Type=procid;
  244.    
  245.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  246.                  mode, 3,
  247.                  c2b(fh), c2b(parent), cstr2b(name,glob->stringbuf) );
  248.    if( res1 ){
  249.       return fh;
  250.    }else{
  251.       dfree( fh );
  252.       return NULL;
  253.    }
  254. }
  255.  
  256.  
  257. struct FileHandle *xOpenFromLock( glb glob, struct FileLock *lock ){
  258.    struct MsgPort *procid;
  259.    LONG res1;
  260.    struct FileHandle *fh;
  261.    
  262.    procid = lock ? lock->fl_Task : glob->xprocid;
  263.    if( !dalloc(fh) ){
  264.       OUTOFMEM;
  265.       return NULL;
  266.    }
  267.    fh->fh_Pos=fh->fh_End=-1L;
  268.    fh->fh_Type=procid;
  269.    
  270.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  271.                  ACTION_FH_FROM_LOCK, 2,
  272.                  c2b(fh), c2b(lock) );
  273.    if( res1 ){
  274.       return fh;
  275.    }else{
  276.       dfree( fh );
  277.         /* Attemp to open file with Open(lock,""). */
  278.         if( fh = xOpen( glob, lock, "", MODE_OLDFILE ) ){
  279.             xUnLock( glob, lock );
  280.             return fh;
  281.         }else{
  282.             struct FileLock *parent;
  283.             struct FileInfoBlock *fib;
  284.             
  285.             /* Attemp to open file with Open(ParentDir(lock),name). */
  286.             if( !dalloc(fib) ){
  287.                 OUTOFMEM;
  288.                 return NULL;
  289.             }
  290.             if( !xExamine( glob, lock, fib ) ){
  291.                 dfree(fib);
  292.                 return NULL;
  293.             }
  294.             if( !(parent = xParentDir( glob, lock )) ){
  295.                 dfree(fib);
  296.                 return NULL;
  297.             }
  298.             if( fh = xOpen( glob, parent, fib->fib_FileName, MODE_OLDFILE )){
  299.                 xUnLock( glob, parent );
  300.                 xUnLock( glob, lock );
  301.                 dfree(fib);
  302.                 return fh;
  303.             }
  304.             xUnLock( glob, parent );
  305.             dfree(fib);
  306.             return NULL;
  307.         }
  308.     }
  309. }
  310.  
  311.  
  312. struct FileHandle *xOpenFromCopyOfLock( glb glob, struct FileLock *lock ){
  313.     struct FileLock *lock2;
  314.     struct FileHandle *fh;
  315.     
  316.     if( !(lock2 = xDupLock( glob, lock )) ){
  317.         return NULL;
  318.     }
  319.     if( !(fh = xOpenFromLock( glob, lock2 )) ){
  320.         xUnLock( glob, lock2 );
  321.         return NULL;
  322.     }
  323.     return fh;
  324. }
  325.  
  326.  
  327. BOOL xClose(glb glob, struct FileHandle *fh){
  328.    struct MsgPort *procid;
  329.    LONG res1;
  330.    
  331.    if( !fh ) return DOSTRUE;
  332.    procid = fh->fh_Type;
  333.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  334.                  ACTION_END, 1,
  335.                  fh->fh_Arg1 );
  336.    if( res1 ) dfree( fh );
  337.    return (BOOL) res1;
  338. }
  339.  
  340.  
  341. LONG xRead(glb glob, struct FileHandle *fh, void *buf, LONG len){
  342.    struct MsgPort *procid;
  343.    LONG res1;
  344.    
  345.    if( !fh ) return 0L;
  346.    procid = fh->fh_Type;
  347.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  348.                  ACTION_READ, 3,
  349.                  fh->fh_Arg1, (LONG)buf, len );
  350.    return res1;
  351. }
  352.  
  353.  
  354. LONG xWrite(glb glob, struct FileHandle *fh, void *buf, LONG len){
  355.    struct MsgPort *procid;
  356.    LONG res1;
  357.    
  358.    if( !fh ) return 0L;
  359.    procid = fh->fh_Type;
  360.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  361.                  ACTION_WRITE, 3,
  362.                  fh->fh_Arg1, (LONG)buf, len );
  363.    return res1;
  364. }
  365.  
  366.  
  367. LONG xSeek(glb glob, struct FileHandle *fh, LONG pos, LONG offset){
  368.    struct MsgPort *procid;
  369.    LONG res1;
  370.    
  371.    if( !fh ) return -1L;
  372.    procid = fh->fh_Type;
  373.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  374.                  ACTION_SEEK, 3,
  375.                  fh->fh_Arg1, pos, offset );
  376.    return res1;
  377. }
  378.  
  379.  
  380. LONG xSetFileSize(glb glob, struct FileHandle *fh, LONG pos, LONG offset){
  381.    struct MsgPort *procid;
  382.    LONG res1;
  383.    
  384.    if( !fh ) return -1L;
  385.    procid = fh->fh_Type;
  386.    /* ToDo: NOTE: The docs say that this call takes the *file handle* in
  387.     * Arg1 (and not the usual fh->fh_arg1). This should be tested! */
  388.    debug(("xSetFileSize: Sending packet..."));
  389.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  390.                  ACTION_SET_FILE_SIZE, 3,
  391.                  c2b(fh), pos, offset );
  392.    debug(("returned! with %ld (ioerr=%ld).\n",res1,glob->ioerr));
  393.    return res1;
  394. }
  395.  
  396.  
  397. BOOL xChangeMode(glb glob, ULONG type, void *obj, ULONG mode){
  398.   struct MsgPort *procid;
  399.   BOOL res1;
  400.  
  401.   if(!obj) return FALSE;
  402.   switch(type){
  403.   case CHANGE_FH:
  404.     procid = ((struct FileHandle *)obj)->fh_Type;
  405.     break;
  406.   case CHANGE_LOCK:
  407.     procid = ((struct FileLock *)obj)->fl_Task;
  408.     break;
  409.   default:
  410.     debug(("Error: xChangeMode(): Bad type value %ld.\n",type));
  411.     glob->ioerr = ERROR_OBJECT_WRONG_TYPE;
  412.     return FALSE;
  413.   }
  414.   res1 = dopkt(&glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  415.            ACTION_CHANGE_MODE, 3,
  416.            type, c2b(obj), mode);
  417.   return res1;
  418. }
  419.  
  420.  
  421. BOOL xInfo(glb glob, struct FileLock *lock, struct InfoData *info){
  422.    struct MsgPort *procid;
  423.    LONG res1;
  424.    
  425.    if( !info ) return DOSFALSE;
  426.    if( !lock ) lock = glob->xrootlock;
  427.    procid = lock->fl_Task;
  428.    res1 = dopkt( &glob->iopkt, procid, glob->ioport, &glob->ioerr, 
  429.                  ACTION_INFO, 2,
  430.                  c2b(lock), c2b(info) );
  431.    return (BOOL) res1;
  432. }
  433.  
  434.  
  435.  
  436. /* ToDo: Must search for doc. on correct behavior in case of a NULL lock. */
  437. /* For now, a null lock is different from a lock to the root dir. */
  438. LONG xSameLock( glb glob, struct FileLock *l1, struct FileLock *l2 ){
  439.    BOOL res1;
  440.    struct MsgPort *prcid1, *prcid2;
  441.    BPTR vol1,vol2;
  442.    
  443. /* debug(("xSameLock(%lx,%lx) ",l1,l2));    */
  444.    prcid1 = l1 ? l1->fl_Task : glob->xprocid;
  445.    prcid2 = l2 ? l2->fl_Task : glob->xprocid;
  446.    if( !l1 || !l2 )     /* Test for a NULL lock. */
  447.       return l1==l2 ? LOCK_SAME :
  448.                       prcid1==prcid2 ? LOCK_SAME_HANDLER : LOCK_DIFFERENT;
  449.    vol1 = l1->fl_Volume;
  450.    vol2 = l2->fl_Volume;
  451.    if( vol1 != vol2 || prcid1 != prcid2 ) return LOCK_DIFFERENT;
  452.    res1 = dopkt( &glob->iopkt, prcid1, glob->ioport, &glob->ioerr, 
  453.                  ACTION_SAME_LOCK, 2,
  454.                  c2b(l1), c2b(l2) );
  455.    if( !res1 && glob->ioerr == ERROR_ACTION_NOT_KNOWN ){
  456.       /* Klugde to support pre-2.0 file systems that cannot handle the
  457.        * SameLock() function. To simulate it, the full paths for both locks
  458.        * are obtained and compared.
  459.        */
  460.       if( !xgetpath( glob, l1, &glob->stringbuf[0], MAXSTRING-1 ) )
  461.          res1 = FALSE;
  462.       else if( !xgetpath( glob, l2, &glob->stringbuf2[0], MAXSTRING-1 ) )
  463.          res1 = FALSE;
  464.       else{
  465.          res1 = !strcmp(&glob->stringbuf[0],&glob->stringbuf2[0]);
  466.          debug(("xSameLock(): '%s' '%s' -> %ld\n",
  467.                 &glob->stringbuf[0],&glob->stringbuf2[0],res1));
  468.       }
  469.    }
  470. /* debug((": result %ld\n",res1)); */
  471.    return res1 ? LOCK_SAME : LOCK_SAME_HANDLER;
  472. }
  473.  
  474.  
  475. /* A strncat() that allows zero or negative max length. */
  476. static char *mystrncat(char *d,char *s,int num){
  477.    if(num>0) return strncat(d,s,num);
  478.    else return d;
  479. }
  480.  
  481.  
  482. /* The recursion part of the xgetpath() function (see below). */
  483. static BOOL xgetpath1(glb glob, struct FileLock *Lock,char *f,
  484.                      struct FileInfoBlock *finfo,int max){
  485.    char fname[31];
  486.    LONG type;
  487.    struct FileLock *parent;
  488.    BOOL result;
  489.    
  490.    if( !xExamine( glob, Lock, finfo) ) return FALSE;
  491.    fname[30]='\0';
  492.    strncpy(fname,finfo->fib_FileName,30);
  493.    type = finfo->fib_DirEntryType;
  494.    if( parent = xParentDir(glob, Lock) ){
  495.       result = xgetpath1( glob, parent, f, finfo, max );
  496.       xUnLock( glob, parent );
  497.       if(!result) return result;
  498.    }
  499.    mystrncat( f, fname, max-strlen(f) );
  500.    if(type>0) mystrncat( f, parent ? "/" : ":",max-strlen(f) );
  501.    return TRUE;
  502. }
  503.  
  504.  
  505. /* Get the full path name of an xlock in the buffer 'f' with lenght max.
  506.  * Returns TRUE if succesfull, FALSE if an error occured.
  507.  */
  508. BOOL xgetpath(glb glob, struct FileLock *Lock, char *f, int max){
  509.    struct FileInfoBlock *finfo;
  510.    BOOL err;
  511.    
  512.    if( !dalloc(finfo) ) return FALSE;
  513.    strcpy(f,"");
  514.    err = xgetpath1( glob, Lock, f, finfo, max);
  515.    dfree(finfo);
  516.    return err;
  517. }
  518.  
  519.  
  520. /* Generation of temporary file names. 
  521.  * Called with a buffer of at least TMPNAMEMAXSIZE bytes.
  522.  */
  523. static void NewTmpFile(glb glob, char *name){
  524.    sprintf(name,TMPNAMETEMPLATE,glob->mytask,glob->tmpfilecount++);
  525. }
  526.  
  527.  
  528. /* Clone protection flags etc. of file (or dir). */
  529. BOOL CloneFile2File(glb glob, struct FileLock *parent1, char *name1,
  530.                       struct FileLock *parent2, char *name2){
  531.   struct FileLock *lock;
  532.  
  533.   if(!(lock = xLock(glob, parent1, name1, ACCESS_READ))){
  534.     debug(("Error: CloneFile2File(): cannot lock file: %ld.\n",glob->ioerr));
  535.     return FALSE;
  536.   }
  537.   if(!xExamine(glob, lock, &glob->fib2)){
  538.     SAVEIOERR;
  539.     debug(("Error: CloneFile2File(): couldn't xExamine(): %ld.\n",glob->ioerr));
  540.     xUnLock(glob, lock);
  541.     RESTIOERR;
  542.     return FALSE;
  543.   }
  544.   xUnLock(glob, lock);
  545.   if(!xSetFileDate(glob, parent2, name2, &glob->fib2.fib_Date)){
  546.     debug(("Error: CloneFile2File(): xSetFileDate / %ld.\n",glob->ioerr));
  547.     return FALSE;
  548.   }
  549.   if(!xSetComment(glob, parent2, name2, &glob->fib2.fib_Comment[0])){
  550.     debug(("Error: CloneFile2File(): xSetComment / %ld.\n",glob->ioerr));
  551.     return FALSE;
  552.   }
  553.   if(!xSetProtection(glob, parent2, name2, glob->fib2.fib_Protection)){
  554.     debug(("Error: CloneFile2File(): xSetProtection / %ld.\n",glob->ioerr));
  555.     return FALSE;
  556.   }
  557.   return TRUE;
  558. }
  559.  
  560.  
  561. /* Transforming a file handle (see TranformFile(), below for details).
  562.  * NOTE BIEN: this function will close the file handle.
  563.  */
  564. BOOL TransformXFH(glb glob, struct FileHandle *srcfh,
  565.      struct FileLock *parent, char *name,
  566.      BOOL (*f)(glb, struct FileHandle *, struct FileHandle *, void *),
  567.      void *userdata){
  568.   char tmp1[TMPNAMEMAXSIZE], tmp2[TMPNAMEMAXSIZE];
  569.   struct FileHandle *dstfh;
  570.   BOOL res;
  571.   DECLIOERR;
  572.   
  573.   NewTmpFile(glob, tmp1);
  574.   if(!(dstfh = xOpen(glob, parent, tmp1, MODE_NEWFILE))){
  575.     debug(("Error: TransFormFile: Cannot open destination '%s': %ld.\n",tmp1,glob->ioerr));
  576.     PUTIOERR;
  577.     goto cleanup_src;
  578.   }
  579.   /* Now do the magic stuff. */
  580.   res = (*f)(glob, srcfh, dstfh, userdata);
  581.   if(!res){
  582.     debug(("Error: TransFormFile: User func returned error: %ld.\n",glob->ioerr));
  583.     PUTIOERR;
  584.     goto cleanup_src_dst;
  585.   }
  586.   if(!xClose(glob, dstfh)){
  587.     /* We can't close the new file - play it safe and keep the old
  588.      * file as it is. */
  589.     debug(("Error: TransFormFile: Error closing new file: %ld.\n",glob->ioerr));
  590.     PUTIOERR;
  591.     goto cleanup_delete_dst;
  592.   }
  593.   /* If we can't close the old file, we'll just have to hope for the 
  594.    * best... (it's a read handle, after all). */
  595.   xClose(glob, srcfh);
  596.   
  597.   /* Now copy the protection flags etc. from the source file. */
  598.   if(!CloneFile2File(glob, parent, name, parent, tmp1)){
  599.     debug(("Error: TransFormFile(): Error cloning file: %ld.\n",glob->ioerr));
  600.     goto failexit;
  601.   }
  602.   
  603.   /* Rename the original file (to make room for the new one). */
  604.   NewTmpFile(glob, tmp2);
  605.   if(!xRename(glob, parent, name, parent, tmp2)){
  606.     /* Something went wrong moving the old file. Better carry on,
  607.      * however, in case the file did move (if it did not we'll fail
  608.      * trying to replace it with the new one).
  609.      */
  610.     debug(("Error: TransFormFile: Couldn't rename the old file: %ld.\n",glob->ioerr));
  611.   }
  612.   if(!xRename(glob, parent, tmp1, parent, name)){
  613.     /* Here comes trouble... we've got the old file away, and the new
  614.      * one won't fall in place. We'll just try to save as much as
  615.      * possible. */
  616.     PUTIOERR;
  617.     if(xRename(glob, parent, tmp2, parent, name)){
  618.       /* We only delete the new file if we are "sure" that the old one
  619.        * is OK. We might end up with two files this way, but it's safer.
  620.        */
  621.       xDeleteFile(glob, parent, tmp1);
  622.     }
  623.     RESTIOERR;
  624.     return FALSE;
  625.   }
  626.   
  627.   /* Great! The new file is well and in place. Let's delete the old
  628.    * one and return (we return succes even if the old file wouldn't go
  629.    * away. */
  630.   xDeleteFile(glob, parent, tmp2);
  631.   return TRUE;
  632.   /*-----------------------------------------------------------------------*/
  633.   
  634.   /* Common clean-up code in error case. Remember to set RESTIOERR. */
  635.  cleanup_src_dst:
  636.   /* Now clean up the mess. Any of these calls may fail; we'll just
  637.    * have to make the best of it. */
  638.   
  639.   /* We try to truncate the file to save flushing buffers. */
  640.   /* This call is switchable, since the docs are dubious... */
  641.   if(glob->truncateonpack){
  642.     xSetFileSize(glob, dstfh, 0L, OFFSET_BEGINNING);
  643.   }else{
  644.     debug(("File truncation not requested... skipping.\n"));
  645.   }
  646.   xClose(glob, dstfh);
  647.  cleanup_delete_dst:
  648.   xDeleteFile(glob, parent, tmp1);
  649.  cleanup_src:
  650.   xClose(glob, srcfh);    /* Leave the file as it was. */
  651.   RESTIOERR;
  652.  failexit:
  653.   return FALSE;
  654. }
  655.  
  656.  
  657. /* The main function for transforming files (typically for packing a
  658.  * file after ACTION_END). Arguments 'parent' and 'name' refer to the
  659.  * file to be transformed. Additionally, a function and data pointer
  660.  * is passed. The function will receive source and destination file
  661.  * handles for it to do its magic on.
  662.  */
  663. BOOL TransformFile(glb glob, struct FileLock *parent, char *name,
  664.      BOOL (*f)(glb, struct FileHandle *, struct FileHandle *, void *),
  665.      void *userdata){
  666.   struct FileHandle *srcfh;
  667.   
  668.   if(!(srcfh = xOpen(glob, parent, name, MODE_OLDFILE))){
  669.     debug(("Error: TransFormFile: Cannot open source: %ld.\n",glob->ioerr));
  670.     return FALSE;
  671.   }
  672.   return TransformXFH(glob, srcfh, parent, name, f, userdata);
  673. }
  674.  
  675.  
  676. /* Get the size of an open file. Returns size or -1L if error.
  677.  * Currently, in case of error, the file position may change.
  678.  */
  679. LONG xGetFileSize(glb glob, struct FileHandle *fh){
  680.   LONG oldpos,size;
  681.   
  682.   /* Todo: Should first try Examine_FH() (and perhaps even try
  683.    * SetFileSize(..,0,OFFSET_END) ). */
  684.   oldpos = xSeek(glob, fh, 0L, OFFSET_END);
  685.   if(oldpos==-1L) return oldpos;
  686.   size = xSeek(glob, fh, oldpos, OFFSET_BEGINNING);
  687.   return size;
  688. }
  689.  
  690.  
  691. /* Write string to output file, returning success / failure. */
  692. BOOL xWriteStr(glb glob, struct FileHandle *fh, char *str){
  693.   LONG len = strlen(str);
  694.  
  695.   if(len != xWrite(glob, fh, str, len)){
  696.     debug(("Error: xWriteStr(): xWrite(): %ld.\n",glob->ioerr));
  697.     return FALSE;
  698.   }
  699.   return TRUE;
  700. }
  701.  
  702.  
  703. /* End of dosfunc.h */
  704.